package com.zareal.ymcc.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.zareal.ymcc.domain.MediaFile;
import com.zareal.ymcc.service.IMediaFileService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup = "course-video-group",
        topic = "video-topic",
        selectorExpression = "push-tag",
        messageModel = MessageModel.CLUSTERING
)
public class VideoPushConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;
    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        String videoStr = new String(body, StandardCharsets.UTF_8);
        // 将String 类型videoStr 转换为MediaFile对象
        MediaFile mediaFile = JSONObject.parseObject(videoStr,MediaFile.class);
        mediaFileService.handleFile2m3u8(mediaFile);
    }
}
